import BaseController from './base';
import { updateLoadFile } from '../oss';

export default class GoodController extends BaseController {

  // 上传商品
  public async create() {
    const { ctx, service } = this;
    try {
      const pid = ctx.helper.nanoid();
      const { body, files } = ctx.request;
      if (files.length === 0) return this.fail(400, '请上传图片！');
      await updateLoadFile(`goods/${pid}.jpg`, files);
      const { name, description, count, price, categoryId } = body;
      if (!name) return this.fail(400, '产品名称不能为空！');
      const options = { name, description, pid, count: parseInt(count), price: parseInt(price), categoryId: parseInt(categoryId) };
      const data = await service.good.create(options);
      this.success(data);
    } catch (error) {
      console.log(error);
    }
  }

  public async update() {
    const { ctx, service } = this;
    try {
      const { goodId } = ctx.params;
      const options = ctx.request.body;
      if (Object.keys(options).length === 0) return this.fail(400, '请输入有效的参数');
      const data = await service.good.update(goodId, options);
      if (data.err) return this.fail(data.status, data.err);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }

  public async getGoods() {
    const { ctx, service } = this;
    try {
      const options = ctx.helper.formatQuery(ctx.query);
      const limit = parseInt(ctx.query.limit) || 10;
      const page = parseInt(ctx.query.page) || 1;
      const orders = ctx.query.orders ? JSON.parse(ctx.query.orders) : [];
      const data = await service.good.getGoods(options, limit, page, orders);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }

  public async searchGoods() {
    const { ctx, service } = this;
    try {
      const { search: searchOption, orders: orderOption, limit: limitOption, page: pageOption } = ctx.query;
      const limit = parseInt(limitOption) || 10;
      const page = parseInt(pageOption) || 1;
      const offset = (page - 1) * limit;
      const search = searchOption ? JSON.parse(searchOption) : {};
      const orders = orderOption ? JSON.parse(orderOption) : [];
      const data = await service.good.searchGoods(search, orders, limit, offset);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }

  public async findGoodById() {
    const { ctx, service } = this;
    try {
      const { id } = ctx.params;
      if (!id) return this.fail(400, '请输入商品ID');
      const data = await service.good.findGoodById(id);
      if (data.err) this.fail(data.status, data.err);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }

  public async removeGood() {
    const { ctx, service } = this;
    try {
      const { goodId } = ctx.params;
      const data = await service.good.removeGood(goodId);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }
}
